package org.example.business;

import org.example.manage.OrderManage;
import org.example.model.Order;
import org.example.ui.customer.HistoryOrderPanel;
import org.example.ui.milk_order.MilkTeaItemPanel;

import javax.swing.*;
import java.awt.*;
import java.util.List;

public class OrderBusiness {
    public static JPanel orderContainer;
    public static MilkTeaItemPanel[] itemPanel;
    public static List<Order> orders;
    public static SpringLayout orderLayout;
    public static void updateOrders() {
        orders = new OrderManage("data/order_file/" + UserBusiness.user.orderPath).read();
        if (orders == null) {
            return;
        }
        orders.reversed();
        orderContainer.removeAll();
        itemPanel = new MilkTeaItemPanel[orders.size()];
        for (int i = 0; i < orders.size(); i++) {
            itemPanel[i] = new MilkTeaItemPanel(orders.get(i));
            itemPanel[i].setBorder(null);
        }
        orderContainer.setPreferredSize(new Dimension(200, orders.size()*140));
        setOrders("" , "");
        orderContainer.setVisible(false);
        orderContainer.setVisible(true);
    }
    public static void setOrders(String conditionA, String conditionB) {
        orderContainer.removeAll();
        int n = 0, num = 0;
        boolean b = true;
        for (int i = 0; i < OrderBusiness.orders.size(); i++) {
            if (!OrderBusiness.orders.get(i).condition.equals(conditionA)
                    &&!OrderBusiness.orders.get(i).condition.equals(conditionB)) {
                orderContainer.add(OrderBusiness.itemPanel[i]);
                if (b) {
                    orderLayout.putConstraint(SpringLayout.SOUTH, OrderBusiness.itemPanel[i],135, SpringLayout.NORTH, orderContainer);
                    orderLayout.putConstraint(SpringLayout.NORTH, OrderBusiness.itemPanel[i],0, SpringLayout.NORTH, orderContainer);
                }
                else {
                    orderLayout.putConstraint(SpringLayout.SOUTH, OrderBusiness.itemPanel[i], 135, SpringLayout.SOUTH, OrderBusiness.itemPanel[n]);
                    orderLayout.putConstraint(SpringLayout.NORTH, OrderBusiness.itemPanel[i], 0, SpringLayout.SOUTH, OrderBusiness.itemPanel[n]);
                }
                orderLayout.putConstraint(SpringLayout.WEST, OrderBusiness.itemPanel[i], 0, SpringLayout.WEST, OrderBusiness.orderContainer);
                orderLayout.putConstraint(SpringLayout.EAST, OrderBusiness.itemPanel[i], 0, SpringLayout.EAST, OrderBusiness.orderContainer);
                b = false;
                n = i;
                num ++;
            }
        }
        orderContainer.setPreferredSize(new Dimension(200, num * 140));
        orderContainer.setVisible(false);
        orderContainer.setVisible(true);
    }
}
